﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WarehouseInsp.Buz.Interfaces;
using Microhard.WarehouseInsp.Buz.DataAccess;
using Microhard.Core.DBUtility;
using Microhard.Core;
using Microhard.WarehouseInsp.Buz.DTO;
using Microhard.WarehouseInsp.Buz.BuzEnum;
using Microhard.JHWorkflowTrans;

namespace Microhard.WarehouseInsp.Buz.Impls
{
    public class OutInPutApplyImpl : IOutInputApply
    {
        public Core.DBUtility.ExecutedResult Add(Models.OutInPutApplyModel model)
        {
            ExecutedResult result = new ExecutedResult();
            WarehouseInspEntities db = new WarehouseInspEntities();

            try
            {
                var o = model.ToEntity();
                db.OutInPutApplies.AddObject(o);
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, "添加出库申请失败");
                Log.Error("OutInPutApplyImpls.Add", ex);
            }
            return result;

        }

        public Core.DBUtility.ExecutedResult Update(Models.OutInPutApplyModel model)
        {
            ExecutedResult reslut = new ExecutedResult();
            WarehouseInspEntities db = new WarehouseInspEntities();
            var oi = db.OutInPutApplies.SingleOrDefault(oip => oip.OutInPutApplyID == model.OutInPutApplyID
                && (oip.State == (int)PrjStatus.RollBack || oip.State == (int)PrjStatus.Saved));
            if (oi != null)
            {
                try
                {
                    oi.PredictOutDate = model.PredictOutDate;
                }
                catch (Exception ex)
                {

                }
            }

            return reslut;
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            WarehouseInspEntities entity = new WarehouseInspEntities();
            ExecutedResult result = new ExecutedResult();
            if (ids != null && ids.Count > 0)
            {
                foreach (Guid g in ids)
                {
                    var OutApply = entity.OutInPutApplies.SingleOrDefault(e => e.OutInPutApplyID == g && e.State == (int)PrjStatus.Saved);
                    if (OutApply != null)
                    {
                        if (OutApply.State == (int)PrjStatus.Saved)
                        {
                            var OutWarehouse = entity.OutInPutWarehouseRecords.Where(e => e.OutInPutApplyID == g);
                            try
                            {
                                if (OutWarehouse != null && OutWarehouse.Count() > 0)
                                {
                                    foreach (var o in OutWarehouse)
                                    {
                                        entity.OutInPutWarehouseRecords.DeleteObject(o);
                                    }
                                }

                                if (OutApply != null)
                                {
                                    entity.OutInPutApplies.DeleteObject(OutApply);
                                }

                                entity.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, "删除出库申请失败");
                                Log.Error("OutInPutApplyImpls.Delete", ex);
                            }
                        }
                    }

                }
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(Models.OutInPutApplyModel model)
        {
            WarehouseInspEntities entity = new WarehouseInspEntities();
            ExecutedResult result = new ExecutedResult();
            if (model.State == (int)PrjStatus.Saved)
            {
                var OutApply = entity.OutInPutApplies.SingleOrDefault(e => e.OutInPutApplyID == model.OutInPutApplyID);
                var OutWarehouse = entity.OutInPutWarehouseRecords.Where(e => e.OutInPutApplyID == model.OutInPutApplyID);
                try
                {
                    if (OutWarehouse != null && OutWarehouse.Count() > 0)
                    {
                        foreach (var o in OutWarehouse)
                        {
                            entity.OutInPutWarehouseRecords.DeleteObject(o);
                        }
                    }

                    if (OutApply != null)
                    {
                        entity.OutInPutApplies.DeleteObject(OutApply);
                    }

                    entity.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, "删除出库申请失败");
                    Log.Error("OutInPutApplyImpls.Delete", ex);
                }
            }
            return result;

        }

        public Models.OutInPutApplyModel GetModelByID(Guid id)
        {
            WarehouseInspEntities entity = new WarehouseInspEntities();
            var apply = entity.OutInPutApplies.SingleOrDefault(e => e.OutInPutApplyID == id).ToModel();
            return apply;
        }

        public Models.OutInPutApplyModel GetModelByModel(Models.OutInPutApplyModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.OutInPutApplyModel> Query(Models.OutInPutApplyModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            WarehouseInspEntities db = new WarehouseInspEntities();
            var apply = from p in db.OutInPutApplies
                        where p.OutInPutApplyID != Guid.Empty
                        && this.scope.PKID.Contains(p.OutInPutApplyID)
                        select p;
            var obj = apply.OrderByDescending(e => e.ApplyDate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            totalCount = apply.Count();
            if (obj != null && obj.Count > 0)
            {
                return obj.Select(m => m.ToModel()).ToList();
            }
            return new List<Models.OutInPutApplyModel>();
        }

        public List<Models.OutInPutApplyModel> Query(Models.OutInPutApplyModel Model)
        {
            WarehouseInspEntities db = new WarehouseInspEntities();
            var apply = from p in db.OutInPutApplies
                        where p.OutInPutApplyID != Guid.Empty
                         && this.scope.PKID.Contains(p.OutInPutApplyID)
                        select p;
            if (apply != null && apply.Count() > 0)
            { return apply.ToList().Select(m => m.ToModel()).ToList(); }
            return new List<Models.OutInPutApplyModel>();
        }

        public Core.DBUtility.ScopeResult scope
        {
            get;
            set;
        }

        public ExecutedResult Start(Guid pkid, Guid wfInstanceID, string nextApprovers)
        {
            ExecutedResult rtn = new ExecutedResult();
            WarehouseInspEntities db = new WarehouseInspEntities();
            var entity = db.OutInPutApplies.SingleOrDefault(e => e.OutInPutApplyID == pkid);
            if (entity != null && (entity.State == (int)PrjStatus.Saved
                || entity.State == (int)PrjStatus.RollBack))
            {
                rtn.ExecutedModel = entity.ToModel();
                entity.State = (int)PrjStatus.Approving;
                entity.ApproverID = nextApprovers;
                entity.ApplyDate = DateTime.Now;
                //获得审批者姓名
                var user = JHWorkflowTool.GetUserByID(nextApprovers);
                if (user != null)
                {
                    entity.ApproverName = user.Name;
                }
                rtn.NextApprovers = nextApprovers;
                //更新出库明细状态
                if (entity.OutInPutWarehouseRecords.Count > 0)
                {
                    foreach (var e in entity.OutInPutWarehouseRecords)
                    {
                        e.Status = (int)PrjStatus.Approving;
                    }
                }
            }
            else
            {
                rtn.ExecutedModel = null;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error("IOutInputApply.Start", ex);
            }
            return rtn;
        }
    }
}
